<?php

/**
 * 退款操作
 */

namespace app\order\common;

class RefundCommon extends \app\member\common\MemberCommon {


    protected function index() {
        $this->pageInfo['name'] = '退款管理';
        $this->setMeta('退款管理');
        $this->setCrumb([
            [
                'name' => '退款管理',
                'url' => url()
            ]
        ]);
        $type = request('get', 'type', 0 , 'intval');
        $urlParam['type'] = $type;
        $where = [];
        $where['A.user_id'] = $this->userInfo['user_id'];
        switch($type) {
            case 1:
                $where['A.status'] = 1;
                break;
            case 2:
                $where['A.status'] = 2;
                break;
            case 3:
                $where['A.status'] = 3;
                break;
            case 4:
                $where['A.status'] = 0;
                break;
        }

        $where = [];

        $model = target('order/OrderRefund');
        $count = $model->countList($where);
        $pageData = $this->pageData($count, 10, $urlParam);
        $list = $model->loadList($where, $pageData['limit'], 'refund_id desc');

        $this->assign('pageList', $list);
        $this->assign('page', $pageData['html']);
        $this->assign('type', $type);
        $this->memberDisplay();
    }

    protected function info() {
        $refundNo = request('', 'refund_no');
        $info = target('order/OrderRefund')->getWhereInfo([
            'refund_no' => $refundNo
        ]);
        if (empty($info)) {
            $this->error('该退款单不存在!');
        }

        $orderInfo = target('order/Order')->getInfo($info['order_id']);

        if ($info['user_id'] <> $this->userInfo['user_id']) {
            $this->error404();
        }

        $this->pageInfo['name'] = '退款详情';
        $this->setMeta('退款详情');
        $this->setCrumb([
            [
                'name' => '退款详情',
                'url' => url()
            ]
        ]);
        $this->assign('info', $info);
        $this->assign('orderInfo', $orderInfo);
        $this->memberDisplay();

    }

    protected function cancel() {
        $refundNo = request('', 'refund_no');
        $info = target('order/OrderRefund')->getWhereInfo([
            'refund_no' => $refundNo
        ]);
        if (empty($info)) {
            $this->error('该退款单不存在!');
        }
        if($info['status'] <> 1) {
            $this->error('该退款申请无法取消!');
        }
        if ($info['user_id'] <> $this->userInfo['user_id']) {
            $this->error('无法取消该订单!');
        }

        target('order/OrderRefund')->beginTransaction();
        $status = target('order/OrderRefund')->edit([
            'refund_id' => $info['refund_id'],
            'status' => 0,
            'process_time' => time()
        ]);
        if (!$status) {
            target('order/OrderRefund')->rollBack();
            $this->error('申请取消失败,请稍后再试!');
        }

        $status = target('order/OrderGoods')->edit([
            'id' => $info['order_goods_id'],
            'service_status' => 0
        ]);
        if (!$status) {
            target('order/OrderRefund')->rollBack();
            $this->error('申请取消失败,请稍后再试!');
        }
        target('order/OrderRefund')->commit();
        $this->success('退款申请取消成功!');

    }

    protected function push() {
        $id = request('', 'id');
        $info = target('order/OrderGoods')->getWhereInfo([
            'id' => $id
        ]);
        if (empty($info)) {
            $this->error('该订单商品不存在');
        }
        if ($info['service_status']) {
            $this->error('该商品正在售后状态!');
        }

        $target = target('order/Order');
        $orderInfo = $target->getInfo($info['order_id']);

        if ($orderInfo['order_user_id'] <> $this->userInfo['user_id']) {
            $this->error404();
        }

        if(!$orderInfo['order_status']) {
            $this->error('该订单已关闭!');
        }

        if($orderInfo['status_data']['action'] <> 'comment' && $orderInfo['status_data']['action'] <> 'complete') {
            $this->error('暂时无法进行退款操作!');
        }


        if (!isPost()) {
            $this->pageInfo['name'] = '商品退款';
            $this->setMeta('商品退款');
            $this->setCrumb([
                [
                    'name' => '商品退款',
                    'url' => url('', ['id' => $id])
                ]
            ]);

            $this->assign('id', $id);
            $this->assign('info', $info);
            $this->assign('orderInfo', $orderInfo);
            $this->assign('causeList', target('order/OrderRefund')->causeList());
            $this->memberDisplay();
        } else {
            $cause = str_len(request('post', 'cause', '', 'html_clear'), 250);
            $content = str_len(request('post', 'content', '', 'html_clear'), 300);
            $money = request('post', 'money', 0, 'price_format');

            if (empty($cause)) {
                $this->error('请选择退款原因!');
            }
            if (empty($content)) {
                $this->error('请填写退款详情!');
            }
            if(empty($money)) {
                $this->error('请填写退款金额!');
            }
            if($money > $info['price_total']) {
                $this->error('退款金额不能大于购买金额!');
            }
            $images = $_POST['images'];
            if (!empty($images) && is_array($images)) {
                $httpHost = 'http://' . trim($_SERVER["HTTP_HOST"], '/');
                foreach ($images as $image) {
                    if (substr($image, 0, 1) <> '/' && strpos($image, $httpHost, 0) === false) {
                        $this->error('您上传的图片有误,请重新上传!');
                    }
                }
            }else {
                $images = [];
            }
            $images = $images ? $images : [];

            target('order/OrderRefund')->beginTransaction();
            $status = target('order/OrderRefund')->add([
                'order_goods_id' => $info['id'],
                'user_id' => $this->userInfo['user_id'],
                'money' => $money,
                'cause' => $cause,
                'content' => $content,
                'images' => serialize($images),
                'status' => 1,
                'create_time' => time(),
                'refund_no' => target('order/Order','service')->logNo()
            ]);
            if (!$status) {
                target('order/OrderRefund')->rollBack();
                $this->error('申请提交失败,请稍后再试!');
            }
            $status = target('order/OrderGoods')->edit([
                'id' => $id,
                'service_status' => 1
            ]);
            if (!$status) {
                target('order/OrderRefund')->rollBack();
                $this->error('申请提交失败,请稍后再试!');
            }
            target('order/OrderRefund')->commit();
            $this->success('退款申请提交成功,等待工作人员审核!', url('index'));
        }

    }


}